﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Qiang.Data;
using Qiang.Topology.Geometries;

namespace Qiang.Styles
{
    public class VectorSelection:Selection,IVectorSelection
    {

        #region 初始化

        /// <summary>
        /// 
        /// </summary>
        public VectorSelection(ShapeFilter filter)
            :base()
        {
            _filter = filter;
        }

        #endregion

        
        #region 属性


        ShapeFilter _filter;

        /// <summary>
        /// 
        /// </summary>
        public ShapeFilter Filter
        {
            get { return _filter; }
            set { _filter = value; }
        }


        /// <summary>
        /// 
        /// </summary>
        public int Count
        {
            get
            {
                int result = 0;
                foreach (ShapeState state in _filter.VectorStates.Values)
                {
                    if (state.IsSelected) result += 1;
                }
                return result;
            }
        }

        #endregion



        #region 方法

        /// <summary>
        /// 
        /// </summary>
        /// <param name="shp"></param>
        public override void Select(IExtent Region)
        {
            Select(Region, SelectionMode.Marquee);
        }
               

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Region"></param>
        /// <param name="Type"></param>
        public override void Select(IExtent Region, SelectionMode Mode)
        {
            Polygon polygon = Region.ToPolygon() as Polygon;
            switch (Mode)
            {
                case SelectionMode.Marquee:
                    foreach (IShape shp in _filter.VectorStates.Keys)
                    {
                        if (Region.Intersects(shp.ToGeometry()))
                            _filter.VectorStates[shp].IsSelected = true;
                    }
                    break;
                case SelectionMode.Click:
                    IEnumerable<IShape> Nodes = from item in _filter.VectorStates.Keys where item.ShapeType == ShapeType.Node select item;
                    foreach (IShape shp in Nodes)
                    {
                        if (polygon.Contains(shp.ToGeometry() as Geometry) == true)
                        {
                            _filter.VectorStates[shp].IsSelected = true;
                            return;
                        }
                    }
                    IEnumerable<IShape> Segments = from item in _filter.VectorStates.Keys where item.ShapeType == ShapeType.Segment select item;
                    foreach (IShape shp in Segments)
                    {
                        if ((shp.ToGeometry() as Geometry).Intersects(polygon) == true)
                        {
                            _filter.VectorStates[shp].IsSelected = true;
                            return;
                        }
                    }
                    IEnumerable<IShape> Polygons = from item in _filter.VectorStates.Keys where item.ShapeType == ShapeType.Polygon select item;
                    foreach (IShape shp in Polygons)
                    {
                        if ((shp.ToGeometry() as Geometry).Contains(new Point(Region.Center)) == true)
                        {
                            _filter.VectorStates[shp].IsSelected = true;
                            return;
                        }
                    }
                    break;
            }
        }

       

        /// <summary>
        /// 
        /// </summary>
        public override void Clear()
        {
            foreach (ShapeState state in _filter.VectorStates.Values)
            {
                if (state.IsSelected) state.IsSelected = false;
            }
            base.Clear();
           
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="shp"></param>
        /// <returns></returns>
        public override bool Contains(IShape shp)
        {
            return false;
        }


        public IList<IShape> ToList()
        {
            List<IShape> shps = new List<IShape>();
            foreach (KeyValuePair<IShape, ShapeState> item in _filter.VectorStates)
            {
                if (item.Value.IsSelected) shps.Add(item.Key);
            }
            return shps;
        }


        #endregion


    }
}
